/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MF1DSolver;

import Integration.IFunction1D;
import MeshlessShapeFunctions.JMeshlessInterpolationFunction1D;
import java.util.ArrayList;
import nonequidistant.FEMShapeFunctions1D.FEMShapeFunction1D1Pow;

/**
 *
 * @author mark_o
 */
public class CoupledResult implements IFunction1D{
    protected double[] results;
    protected ArrayList<JMeshlessInterpolationFunction1D> MeshlessShapeList;
    protected double divPoint;
    protected ArrayList<Double> ListOfFEMPoints;
    protected ArrayList<FEMShapeFunction1D1Pow> FuncList;

    public CoupledResult(double[] results, ArrayList<JMeshlessInterpolationFunction1D> MeshlessShapeList, double divPoint, ArrayList<Double> ListOfFEMPoints) {
        this.results = results;
        this.MeshlessShapeList = MeshlessShapeList;
        this.divPoint = divPoint;
        this.ListOfFEMPoints = ListOfFEMPoints;
        generateFunctions();
    }

    private void generateFunctions(){
        FuncList = new ArrayList<FEMShapeFunction1D1Pow>();
        FuncList.add(new FEMShapeFunction1D1Pow(ListOfFEMPoints.get(0), 0.01,ListOfFEMPoints.get(1) - ListOfFEMPoints.get(0)));
        for (int i = 1; i < ListOfFEMPoints.size()-1; i++) {
            double h1 = ListOfFEMPoints.get(i) - ListOfFEMPoints.get(i-1);
            double h2 = ListOfFEMPoints.get(i+1) - ListOfFEMPoints.get(i);
            FuncList.add(new FEMShapeFunction1D1Pow(ListOfFEMPoints.get(i), h1,h2));
        }
        int n = ListOfFEMPoints.size()-1;
        FuncList.add(new FEMShapeFunction1D1Pow(ListOfFEMPoints.get(n), ListOfFEMPoints.get(n) - ListOfFEMPoints.get(n-1), 0.01));
    }

    public double calculate(double x) {
        double result = 0.0;

        if(x<=divPoint){
            FEMShapeFunction1D1Pow fEMShapeFunction1D1Pow = null;
            for (int i = 0; i < FuncList.size(); i++) {
                fEMShapeFunction1D1Pow = FuncList.get(i);
                result+= results[i]*fEMShapeFunction1D1Pow.calculate(x);
            }
        }else{
        	double tmp = 0.0;
        	int N = this.FuncList.size();
            for (int j = 0; j <MeshlessShapeList.size(); j++) {
                tmp+= MeshlessShapeList.get(j).calculate(x)*results[j+N];
            }
            result = tmp;
        }



        return result;
    }

    public double calculateDerivaty(double x) {
        double result = 0.0;

        if(x<=divPoint){
            FEMShapeFunction1D1Pow fEMShapeFunction1D1Pow = null;
            for (int i = 0; i < FuncList.size(); i++) {
                fEMShapeFunction1D1Pow = FuncList.get(i);
                result+= results[i]*fEMShapeFunction1D1Pow.calculateDerivaty(x);
            }
        }else{
            for (int j = 0; j <MeshlessShapeList.size(); j++) {
                result += MeshlessShapeList.get(j).calculateDiffuseDerivaty(x)*results[j+ListOfFEMPoints.size()];
            }
        }



        return result;
    }

    public ArrayList<FEMShapeFunction1D1Pow> getFuncList() {
        return FuncList;
    }

    public ArrayList<JMeshlessInterpolationFunction1D> getMeshlessShapeList() {
        return MeshlessShapeList;
    }

    public double getDivPoint() {
        return divPoint;
    }



}